﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CommonSense.UI.MVVM.ViewModels
{
    public class RelayCommand : ICommand
    {
        private Func<bool> canExcecuteChangedDelegate;
        private Action executeDelegate;

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return this.canExcecuteChangedDelegate();
        }

        public void Execute(object parameter)
        {
            this.executeDelegate();
        }

        public RelayCommand(Func<bool> canExcecuteChangedDelegate, Action executeDelegate)
        {
            this.canExcecuteChangedDelegate = canExcecuteChangedDelegate;
            this.executeDelegate = executeDelegate;
        }

        public RelayCommand(Action executeDelegate)
        {
            this.canExcecuteChangedDelegate = () => true;
            this.executeDelegate = executeDelegate;
        }

        public void NotifyCanExecuteChanged()
        {
            if (this.CanExecuteChanged != null)
            {
                this.CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
}
